package me.luraframework.auth.security;

import lombok.RequiredArgsConstructor;
import me.luraframework.auth.security.dto.JwtUserDto;
import me.luraframework.commons.utils.JsonUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor
public class OnlineService {

    private final StringRedisTemplate redisTemplate;
    private static final String ONLINE_TOKEN_KEY_PREFIX_QUERY = "online-token-";


    public void addUser(String token, JwtUserDto user) {
        redisTemplate.opsForValue().set(ONLINE_TOKEN_KEY_PREFIX_QUERY + token, JsonUtils.toStr(user));
    }

    public void removeUser(String token) {
        redisTemplate.delete(ONLINE_TOKEN_KEY_PREFIX_QUERY + token);
    }

    public JwtUserDto getOne(String token) {
        return JsonUtils.toObj(redisTemplate.opsForValue().get(ONLINE_TOKEN_KEY_PREFIX_QUERY + token), JwtUserDto.class);
    }
}
